package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.WumingErrorCode;
import com.wuming.house.dao.GoodsCateMapper;
import com.wuming.house.dao.WeixinUserMapper;
import com.wuming.house.model.GoodsCate;
import com.wuming.house.model.WeixinUser;
import com.wuming.house.service.WeixinUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("weixinUserService")
public class WeixinUserServiceImpl extends BaseServiceImpl implements WeixinUserService {

    @Autowired
    private WeixinUserMapper weixinUserMapper;

    @Override
    public void saveWeixinUser(WeixinUser user) throws BusinessException {
        try {
            weixinUserMapper.insert(user);
        } catch (Exception e) {
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public void modifyWeixinUser(WeixinUser user) throws BusinessException {
        try {
            weixinUserMapper.update(user);
        } catch (Exception e) {
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public WeixinUser getUserBySkey(String skey, String openId) throws BusinessException {
        Map<String, String> params = new HashMap<String, String>();
        if (StringUtils.isNotBlank(skey)) {
            params.put("skey", skey);
        }
        if (StringUtils.isNotBlank(openId)) {
            params.put("openId", openId);
        }
        try {
            List<WeixinUser> users = weixinUserMapper.selectByParams(params);
            if (null != users && !users.isEmpty()) {
                return users.get(0);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotalByTime(String startTime, String endTime) throws BusinessException {
        Map<String, String> params = new HashMap<String, String>();
        if (StringUtils.isNotBlank(startTime)) {
            params.put("startTime", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            params.put("endTime", endTime);
        }
        try {
            List<WeixinUser> users = weixinUserMapper.selectByParams(params);
            if (null != users) {
                return users.size();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
        return 0;
    }

    @Override
    public List<WeixinUser> getWeixinUserByPage(String name, String mobile, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(name)) {
            params.put("realname", name);
        }
        if (StringUtils.isNotBlank(mobile)) {
            params.put("mobile", mobile);
        }
        try {
            return (List<WeixinUser>) this.getPageList(WeixinUserMapper.class, "selectByParams", params, pageIndex, pageSize);
        } catch (Exception e) {
            logger.error("分页查询微信用户失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(String name, String mobile) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(name)) {
            params.put("realname", name);
        }
        if (StringUtils.isNotBlank(mobile)) {
            params.put("mobile", mobile);
        }
        try {
            return this.getTotal(WeixinUserMapper.class, "selectTotal", params);
        } catch (Exception e) {
            logger.error("分页查询微信用户总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public WeixinUser getUserByUserid(Long userid) {
        try {
            return weixinUserMapper.selectByPrimaryKey(userid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
